﻿using Microsoft.AspNetCore.Mvc;

namespace Step4.Unit3.SystemMiddleWare.Controllers
{
    //[Route("Product")]
    [Route("[controller]/[action]")]                // 控制器或者Action，一旦拥有了属性路由，就不能被设置为"默认路由页面"了
    public class ProductController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly ISession _session;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public ProductController(
            ILogger<HomeController> logger,
            IHttpContextAccessor httpContextAccessor)
        { 
            _logger = logger;
            _httpContextAccessor = httpContextAccessor;
            _session = _httpContextAccessor.HttpContext!.Session; // 注入时，通过上下文获取HTTP请求的Session
        }

        public IActionResult Index()
        {
            //ProductInfo info = null;
            //_logger.LogInformation(info.Id.ToString());
            _logger.LogInformation("从Session中拿到：'username:"+_session.GetString("username")+"'");

            var sessionID=_session.Id; // 会话Id

            return View();
        }
    }

    public record ProductInfo(int Id,string Name);
}
